<?php

use Illuminate\Database\Seeder;

class TestSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        \App\Models\Node::query()->truncate();
        //创建一些角色
        $roles = [
            [
                'name' => 'admin',
                'display_name' => '管理员',
            ],
            [
                'name' => 'manager',
                'display_name' => '经理',
            ],
            [
                'name' => 'staff',
                'display_name' => '员工'
            ],
        ];
        foreach ($roles as $role){
            \App\Models\Role::create($role);
        }
        //创建一些用户
        $users = [
            [
                'name' => 'admin',
                'password' => bcrypt('123456'),
                'nickname' => '系统管理员',
                'phone' => '13000000000',
            ],
            [
                'name' => 'test',
                'password' => bcrypt('123456'),
                'nickname' => '测试人员',
                'phone' => '13111111111',
            ],
        ];
        foreach ($users as $user){
            \App\Models\User::create($user);
        }
        //创建一些节点
        $nodes = [
            [
                'name' => '完结',
                'sort' => 99,
                'type' => 1,
            ],
            [
                'name' => '初步沟通',
                'sort' => 1,
                'type' => 2,
            ],
            [
                'name' => '邀约上门',
                'sort' => 2,
                'type' => 2,
            ],
            [
                'name' => '签合同',
                'sort' => 3,
                'type' => 2,
            ],
            [
                'name' => '办营业执照',
                'sort' => 1,
                'type' => 3,
            ],
            [
                'name' => '刻章',
                'sort' => 2,
                'type' => 3,
            ],
            [
                'name' => '报税',
                'sort' => 3,
                'type' => 3,
            ],
        ];
        foreach ($nodes as $node){
            \App\Models\Node::create($node);
        }
        //创建一些客户信息
        factory(\App\Models\Customer::class)->times(300)->make()->each(function ($model) {
            // 数据入库
            $model->save();
        });

        //产品业务
        \App\Models\Product::query()->truncate();
        $datas = [
            [
                'name' => '货款',
                'description' => '各种货款',
                'price' => null,
                'parent_id' => 0,
                'child' => [
                    [
                        'name' => '信用货',
                        'description' => '个人征信',
                        'price' => 500,
                    ],
                    [
                        'name' => '抵押贷',
                        'description' => '抵押物品',
                        'price' => 1000,
                    ],
                ]
            ],
            [
                'name' => '会计',
                'description' => '会计服务',
                'price' => null,
                'parent_id' => 0,
                'child' => [
                    [
                        'name' => '报税',
                        'description' => '报税',
                        'price' => 99,
                    ],
                    [
                        'name' => '做帐',
                        'description' => '做帐',
                        'price' => 200,
                    ],
                ]
            ]
        ];
        foreach ($datas as $data){
            $item = \App\Models\Product::create([
                'name' => $data['name'],
                'description' => $data['description'],
                'price' => $data['price'],
                'parent_id' => 0,
            ]);
            if (isset($data['child']) && !empty($data['child'])){
                foreach ($data['child'] as $d){
                    \App\Models\Product::create([
                        'name' => $d['name'],
                        'description' => $d['description'],
                        'price' => $d['price'],
                        'parent_id' => $item->id,
                    ]);
                }
            }
        }


        //创建一些部门
        \App\Models\Department::query()->truncate();
        $datas = [
            [
                'name' => '总公司',
                'level' => 1,
                'child' => [
                    [
                        'name' => '商务一部',
                        'level' => 2,
                    ],
                    [
                        'name' => '商务二部',
                        'level' => 2,
                    ],
                    [
                        'name' => '商务三部',
                        'level' => 2,
                    ],
                    [
                        'name' => '后台一部',
                        'level' => 2,
                    ],
                ]
            ],
        ];
        foreach ($datas as $data){
            $item = \App\Models\Department::create([
                'name' => $data['name'],
                'level' => $data['level'],
                'parent_id' => 0,
            ]);
            if (isset($data['child']) && !empty($data['child'])){
                foreach ($data['child'] as $d){
                    \App\Models\Department::create([
                        'name' => $d['name'],
                        'level' => $d['level'],
                        'parent_id' => $item->id,
                    ]);
                }
            }
        }
    }
}
